package demo1;

class Money implements Cloneable{
    public double m = 19.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable {
    public String name;
    public int age;
    public Money money = new Money();


    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person)super.clone();
        tmp.money = (Money)this.money.clone();
        return tmp;
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person("zhangsan",10);

        //此时新创将一个对象，并将person1内容拷贝给新对象
        Person person2 = (Person) person1.clone();//此时需要调用克隆方法
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
        System.out.println("============");
        person1.money.m = 200;

        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
    }
}
